package twentytwentyone_3;

import java.util.*;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2021-03-07 9:28
 * @Modified By:
 */
public class ConstructRectangle_492 {

    public static void main(String [] args){
        System.out.println(Arrays.toString(constructRectangle(37)));
    }

    public static int[] constructRectangle(int area) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 1; i <= area; i++) {
            if (area % i == 0){
                // map用来保存W和L  K为W，V为L
                map.put(i,area / i);
            }
        }
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        if (entries.size() == 0){
            return null;
        }
        Iterator<Map.Entry<Integer, Integer>> iterator = entries.iterator();
        int min = Integer.MAX_VALUE,key = 0;
        for (Map.Entry<Integer, Integer> entry : entries) {
            int difference = Math.abs(entry.getKey() - entry.getValue());
            if (difference < min && entry.getKey() >= entry.getValue()){
                min = difference;
                key = entry.getKey();
            }
        }
        return new int[]{key,map.get(key)};
    }
}
